$(document).ready(function() {
	//登录页面
	$('#login').click(function() {
		var ck_rmbUser = document.getElementById('ck_rmbUser').checked;
		var myreg = /^[1][3,4,5,7,8][0-9]{9}$/;
		$("#tel_1").focus(function() {
			$("#errTel_1").hide()
		});
		$("#pwd_1").focus(function() {
			$("#errPwd_1").hide()
		});
		//下次自动登录
		if($.cookie("rmbUser") == "true") {
			$("#ck_rmbUser").attr("checked", true);
			$("#tel_1").val($.cookie("username"))
			$("#pwd_1").val($.cookie("password"))
			var datas = {
				'bind_phone': $("#tel_1").val(),
				'password': $("#pwd_1").val()
			}
		} else {
			var datas = {
				'bind_phone': $('#tel_1').val(),
				'password': $('#pwd_1').val()
			}
		};
		if($('#tel_1').val() == '' && $('#Pwd').val() == '') {
			$('#errTel_1').text("*请输入手机号码")
			$('#errTel_1').show()
			$('#errPwd_1').show()
			return false
		} else if($('#tel_1').val() == '') {
			$('#errTel_1').text("*请输入手机号码")
			$('#errTel_1').show()
			return false
		} else if(!myreg.test($('#tel_1').val())) {
			$('#errTel_1').text("*手机号码格式不对")
			$('#errTel_1').show()
			return false
		} else if($('#pwd_1').val() == '') {
			$('#errPwd_1').show()
			return false
		} else if($('#pwd_1').val().length < 6) {
			$('#errPwd_1').text("*密码为6~20位字母或者数字")
			$('#errPwd_1').show()
			return false
		}
		var data = JSON.stringify(datas);
		$.ajax({
			type: "post",
			url: base+'user/loginUser',
			data: data,
			dataType: 'json',
			contentType: "application/json ",
			success: function(data) {
				if(data.code == 0) {
					console.log(data.data)
					var token = JSON.stringify(data.data);
					sessionStorage.setItem('token', token);
					console.log(token)
					var useraccount = data.data.useraccount
					window.location.href = "index-logged.html";
				} else {
					alert("登录失败！");
				}
			}
		});
	});

	//记住用户名密码
	function Save() {
		if(ck_rmbUser) {
			var str_username = $("#tel_1").val();
			var str_password = $("#pwd_1").val();
			$.cookie("rmbUser", "true", {
				expires: 7
			}); //存储一个带7天期限的cookie
			$.cookie("username", str_username, {
				expires: 7
			});
			$.cookie("password", str_password, {
				expires: 7
			});
		} else {
			$.cookie("rmbUser", "false", {
				expire: -1
			});
			$.cookie("username", "", {
				expires: -1
			});
			$.cookie("password", "", {
				expires: -1
			});
		}
	}
});